home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / regexp3.zip / TEST.C < prev   
Text File  |  1994-01-21  |  3KB  |  77 lines

  1. /* test.c -- dap@kandy.com 01/21/94 */
  2. /*
  3.    syntax: test "string" "regular expression"
  4.  
  5.    example: test "hello world" "lo.*"
  6.    the above example should match.
  7.  
  8.    This program won't give the regular expression functions a complete
  9.    workout, but should serve to show whether or not they are completely
  10.    broken, as well as demonstrate how to hook up the functions in your
  11.    program.
  12.  
  13.    Read regexp.man for further info on regular expressions. Regular
  14.    expressions are a complex subject, one that entire textbooks have
  15.    been written about. Becoming a regular expressions wizard is not a
  16.    trivial undertaking, but OTOH, anyone with a little appreciation of
  17.    how regular expressions work can make their life a whole lot easier.
  18.    Basically, they are "wildcards" with jet power and afterburners.
  19.  
  20.    Compile and link with regexp.obj , regsub.obj , and regerror.obj
  21.    Or, roll the above 3 obj files into a .lib , and link with that.
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include "regexp.h"
  27.  
  28. char buf[1024];
  29.  
  30. main(int argc, char **argv)
  31. {
  32.   regexp *ourpointer; /* Our pointer to a struc of type regexp */
  33.  
  34.   /* Before we can use a regular expression, we have to compile it
  35.      with regcomp(), which will malloc a structure of type regexp,
  36.      and return a pointer to it. Note the associated free() statement
  37.      at the end of this program.
  38.  
  39.      If regcomp() fails, it automatically generates an error message
  40.      to stderr, then returns NULL.
  41.   */
  42.   if((ourpointer=regcomp(argv[2]))==NULL)
  43.     exit(1);
  44.  
  45.   /* We can now test for a match by calling regexec() using the pointer
  46.      to our compiled expression, and a pointer to the string.
  47.  
  48.      regexec() returns 1 for success, or 0 for failure.
  49.   */
  50.  
  51.   if(regexec(ourpointer, argv[1]))
  52.   {
  53.     printf("regexec() found a match\n");
  54.     /* Once regexec() succeeds, we can get to the matched substring two
  55.        ways. The first is simply to use the pointers to the beginning
  56.        and end of the substring.
  57.     */
  58.     printf("Printing string using pointers: ");
  59.     fwrite(ourpointer->startp[0], sizeof (char),
  60.        (size_t)(ourpointer->endp[0] - ourpointer->startp[0]),
  61.        stdout);
  62.     /* The second is to do a copy-and-substitute using regsub().
  63.     */
  64.     regsub(ourpointer,
  65.        "\nregsub() says that `&' is the substring, and so is `\\0'!",
  66.        buf);
  67.     printf("%s\n", buf);
  68.   }
  69.   else
  70.   {
  71.     printf("regexec() did not find a match\n");
  72.   }
  73.  
  74.   /* Free the structure that regcomp() malloc'ed */
  75.   free(ourpointer);
  76. }
  77.